package com.funmi.lizip.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.PageRequest;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.entity.Battery;
import com.funmi.lizip.entity.SolarDevice;
import com.funmi.lizip.entity.vo.BatteryVo;
import com.funmi.lizip.service.BatteryService;
import com.funmi.lizip.service.SolarDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "设备电池信息")
@RestController
@RequestMapping("/battery")
public class BatteryController {
    
    @Autowired
    private BatteryService batteryService;
    @Autowired
    private SolarDeviceService solarDeviceService;

    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "分页获取 设备电池信息")
    @GetMapping("")
    public R position(PageRequest request){
        long current = 1;
        long size = 10;
        if (request != null){
            current = request.getPage();
            size = request.getPageSize();
        }
        Page<Battery> batteryPage = batteryService.page(new Page<>(current, size));
        Page<BatteryVo> batteryVoPage = new PageDTO(batteryPage.getCurrent(), batteryPage.getSize(), batteryPage.getTotal());
        List<BatteryVo> batteryVoList = batteryPage.getRecords().stream().map(battery -> {
            BatteryVo batteryVo = new BatteryVo();
            BeanUtils.copyProperties(battery, batteryVo);
            return batteryVo;
        }).collect(Collectors.toList());
        batteryVoPage.setRecords(batteryVoList);
        return R.success().data(batteryVoPage);
    }


    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "分页获取 中继电池管理")
    @GetMapping("/solarDevice")
    public R solarDevice(PageRequest request){
        long current = 1;
        long size = 10;
        if (request != null){
            current = request.getPage();
            size = request.getPageSize();
        }
        Page<SolarDevice> batteryPage = solarDeviceService.page(new Page<>(current, size));
        return R.success().data(batteryPage);
    }

}
